/**
 * Created by huangzuizui on 2018/3/25.
 */

export default class AudioPlayer {
  constructor(src, _, callback) {
    this.audio = window.document.createElement('audio');
    this.audio.src = src;
    this.audio.addEventListener('canplaythrough', callback, false);
  }
  play(callback) {
    if (!this.audio) return;
    this.audio.play();
    this.audio.addEventListener('ended', callback, false);
  }
  pause() {
    if (!this.audio) return;
    this.audio.pause();
  }
  stop() {
    if (!this.audio) return;
    this.audio.currentTime = 0;
    this.audio.pause();
  }
  release() {
    if (!this.audio) return;
    this.stop();
    this.audio = null;
  }
  setCurrentTime(val) {
    if (!this.audio) return;
    this.audio.currentTime = val;
  }
  getCurrentTime(cb) {
    if (!this.audio) return;
    return cb(this.audio.currentTime);
  }
  setNumberOfLoops(val) {
    if (!this.audio) return;
    this.audio.loop = !!val;
  }
  getNumberOfLoops(val) {
    if (!this.audio) return;
    return this.audio.loop;
  }
  setVolume(val) {
    if (!this.audio) return;
    this.audio.volume = val;
    // this.audio.stop();
  }
  getVolume() {
    return this.audio.volume;
  }
  setPan() {
    if (!this.audio) return;
    // this.audio.stop();
  }
  getDuration() {
    if (!this.audio) return;
    return this.audio.duration;
  }
  getNumberOfChannels() {
    // ...
  }
}
